<template>
  <div>
    <h1 style="color: green"></h1>
    <el-container style="height: 500px; border: 1px solid #eee">
      <el-container>

        <el-main>
          <div style="text-align: left" class="div1">
            <br />
            <span>用户名称:</span>
           <el-input placeholder="请输入用户名称" v-model="input1" style="width:200px;margin-right:20px"></el-input>
            <span>手机号:</span>
            <el-input placeholder="请输入手机号" v-model="input2" style="width:200px;margin-right:20px"></el-input>
            <span>状态:</span>
             <el-select v-model="input3" placeholder="状态">
                  <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
                  </el-option>
            </el-select>
            <span class="demonstration">创建时间:</span>
            <el-date-picker
                v-model="value1"
                type="daterange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期">
            </el-date-picker>
            </br>
            <!--查询-->
            <el-button type="info" icon="el-icon-search" @click="select()">查询</el-button>
            <!--添加按钮-->
            <el-button type="success" icon="el-icon-edit" @click="dialogVisible = true">新增</el-button>

            <!--添加模态框-->
            <el-dialog title="新增信息" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
              <span>新增信息</span>
              <br />
              <div class="addinput">
                <el-input placeholder="用户昵称" v-model="adduserName" style="width: 300px"></el-input>
                <el-input placeholder="手机号" v-model="addphone" style="width: 300px"></el-input>
                <el-input placeholder="邮箱" v-model="addemail" style="width: 300px"></el-input>
                <el-input placeholder="用户名称" v-model="addloginName" style="width: 300px"></el-input>
                <el-input placeholder="用户密码" v-model="addpassword" style="width: 300px"></el-input>
                <el-select v-model="addstuSex" placeholder="性别">
                  <el-option v-for="item in options1" :key="item.value" :label="item.label" :value="item.value">
                  </el-option>
                </el-select>
                <el-input type="textarea" :rows="3" placeholder="请输入内容" v-model="addremark">
                </el-input>
              </div>
              <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="save(), (dialogVisible = false)">确 定</el-button>
              </span>
            </el-dialog>

            <!--反显模态框-->
            <el-dialog title="修改信息" :visible.sync="updialogVisible" width="30%" :before-close="handleClose">
              <span>新增信息</span>
              <br />
              <div class="upinput">
                <el-input placeholder="用户昵称" v-model="upuserName" style="width: 300px"></el-input>
                <el-input placeholder="手机号" v-model="upphone" style="width: 300px"></el-input>
                <el-input placeholder="邮箱" v-model="upemail" style="width: 300px"></el-input>
                <el-input placeholder="用户名称" v-model="uploginName" style="width: 300px"></el-input>
                <el-select v-model="upsex" placeholder="性别">
                  <el-option v-for="item in options1" :key="item.value" :label="item.label" :value="item.value">
                  </el-option>
                </el-select>
                <el-input type="textarea" :rows="3" placeholder="请输入内容" v-model="upremark">
                </el-input>
              </div>
              <span slot="footer" class="dialog-footer">
                <el-button @click="updialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="update(), (updialogVisible = false)">确 定</el-button>
              </span>
            </el-dialog>

          </div>


          <el-table :data="tableData">
            <el-table-column prop="id" label="id" width="120">
            </el-table-column>
            <el-table-column prop="userName" label="用户名称" width="120">
            </el-table-column>
            <el-table-column prop="loginName" label="登陆名称" width="140">
            </el-table-column>
            <el-table-column prop="phone" label="手机号" width="120">
            </el-table-column>
            <template slot-scope=""><!-- 类型转换 -->
              <el-table-column label="状态" width="180">
                <template slot-scope="scope">
                  <span v-html="status1(scope.row.status)"></span>
                </template>
              </el-table-column>
            </template>
            <template slot-scope=""><!-- 时间转换 -->
              <el-table-column label="创建时间" width="400">
                <template slot-scope="scope">
                  <span v-html="getYMDHMS(scope.row.createTime)"></span>
                </template>

              </el-table-column>
            </template>

            <el-table-column label="操作">
              <template slot-scope="scope">
                <!--修改按钮-->
                <el-button type="text" @click="find(scope.row.id),(updialogVisible = true)">修改</el-button>
                <!--删除按钮-->
                <el-button size="mini" type="danger"  @click="del(scope.row.id)">删除</el-button>

              </template>
            </el-table-column>
          </el-table>
          <!-- 分页 -->
          <el-row>
            <el-col style="text-align: right">
              <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageNum"
                             :page-sizes="[5, 10, 15, 20]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper"
                             :total="total">

              </el-pagination>
            </el-col>
          </el-row>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  name: "user",
  //页面自动加载，
  created: function () {
    this.select();
  },
  data() {
    return {
      //分页
      pageNum: 1, //默认当前页数
      pageSize: 5, //默认每页显示条数
      total: 0, //总数据条数

      tableData: [],
      input1: "",
      input2: "",
      input3: "",
      value1: "",
      options: [
        {
          value: "0",
          label: "正常",
        },
        {
          value: "1",
          label: "停用",
        },
      ],
      dialogVisible : false,
      textarea: '',
      adduserName : "",
      addphone : "",
      addemail : "",
      addloginName : "",
      addpassword : "",
      addstuSex : "",
      addremark : "",
      options1: [
        {
          value: "0",
          label: "男",
        },
        {
          value: "1",
          label: "女",
        },
      ],
      updialogVisible : false,
        upuserName : "",
      upphone : "",
      upemail : "",
      uploginName : "",
      uppassword : "",
      upsex : "",
      upremark : "",
      upid : "",
    };
  },
  props: {},
  methods: {
    select : function(){
      var that = this;
      this.$axios.post("/sysUser/quest",{
        userName : this.input1,
        phone : this.input2,
        status : this.input3,
        createTime : this.value1,
        //分页
        pageNum: that.pageNum,
        pageSize: that.pageSize,
      })
          .then(function(response){
                that.tableData = response.data.data;
                that.total = response.data.pageInfo.total;
              }
          )
    },
    save : function(){
      var that = this;
      this.$axios.post("/sysUser/save",{
        userName : that.adduserName,
        phone: that.addphone,
        email: that.addemail,
        loginName: that.addloginName,
        password: that.addpassword,
        stuSex: that.addstuSex,
        remark: that.addremark,

      })
          .then(function(){
            that.select(),
                that.$message({
                  //弹出框
                  message: "新增成功!",
                  type: "success",
                });
          })
    },
    del : function(id){
      var that = this;
      this.$axios.post("/sysUser/del",{
        id : id

      }).then(function(){
        that.select(),
            that.$message({
              //弹出框
              message: "删除成功!",
              type: "success",
            });
      })
    },
    find : function(id){
      var that = this;
      this.$axios.post("/sysUser/find",{
        id : id
      }).then(function(response){
        if (response.data.code == "3333") {
          that.upuserName = response.data.data.userName;
          that.upphone = response.data.data.phone;
          that.upemail = response.data.data.email;
          that.uploginName = response.data.data.loginName;
          that.upid = response.data.data.id;
          that.upsex = response.data.data.sex;
          that.upremark = response.data.data.remark;

        } else {
          //alert("修改失败");
        }
      })
    },

    update : function(){
      var that = this;
      this.$axios.post("/sysUser/update",{
        userName : that.upuserName,
        phone : that.upphone,
        email : that.upemail,
        loginName : that.uploginName,
        id :that.upid,
        sex : that.upsex,
        remark : that.upremark,
      }).then(function(){
        that.select();
      })
    },
    // 获取当前页的数据信息
    //pageSize 默认每页显示条数改变时会触发
    handleSizeChange(val) {
      this.pageSize = val;
      this.select(this.pageNum, this.pageSize); //console.log(`每页 ${val} 条`);
    },//currentPage 默认当前页数改变时会触发
    handleCurrentChange(val){
      this.pageNum = val;
      this.select(this.pageNum, this.pageSize); //console.log(`当前页: ${val}`);
    },
    //时间格式转换
    getYMDHMS: function (timestamp) {
      let time = new Date(timestamp);
      let year = time.getFullYear();
      let month = time.getMonth() + 1;
      let date = time.getDate();
      let hours = time.getHours();
      let minute = time.getMinutes();
      let second = time.getSeconds();

      if (month < 10) {
        month = "0" + month;
      }
      if (date < 10) {
        date = "0" + date;
      }
      if (hours < 10) {
        hours = "0" + hours;
      }
      if (minute < 10) {
        minute = "0" + minute;
      }
      if (second < 10) {
        second = "0" + second;
      }return (
          year +
          "-" +
          month +
          "-" +
          date +
          " " +
          hours +
          ":" +
          minute +
          ":" +
          second
      );
    },
    //类型转换
    status1(status) {
      if (status == "0") {
        return "正常";
      } else if (status == "1") {
        return "停用";
      }
    },
  },
};
</script>

<style></style>
